﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace HandRise.MBHOP.Controls
{
    public class ButtonCollection : Control
    {
        private int refreshflag = 0;
        private ImageList itemUpImage = null;
        /// <summary>
        /// 弹起时的图片列表
        /// </summary>
        public ImageList ItemUpImage
        {
            get
            {
                return itemUpImage;
            }
            set
            {
                if (itemUpImage != value)
                {
                    itemUpImage = value;
                    refreshflag = 0;
                    this.Refresh();
                }
            }
        }

        private ImageList itemDowmImage = null;
        /// <summary>
        /// 按下时的图片列表
        /// </summary>
        public ImageList ItemDownImage
        {
            get
            {
                return itemDowmImage;
            }
            set
            {
                if (itemDowmImage != value)
                {
                    itemDowmImage = value;
                    refreshflag = 0;
                    this.Refresh();
                }
            }
        }
        private ImageList itemDisableImage = null;
        /// <summary>
        /// 禁用图片列表
        /// </summary>
        /// 
        public ImageList ItemDisableImage
        {
            get
            {
                return itemDisableImage;
            }
            set
            {
                if (itemDisableImage != value)
                {
                    itemDisableImage = value;
                    refreshflag = 0;
                    this.Refresh();
                }
            }
        }

  

        /// <summary>
        /// 第一按钮的使能

        /// </summary>
        /// 
        private bool itemEnable1 = true;
        public bool ItemEnable1
        {
            get
            {
                return itemEnable1;
            }
            set
            {
                itemEnable1 = value;
                refreshflag =1;
                this.Refresh();
         
            }
        }
        /// <summary>
        /// 第二按钮的使能

        /// </summary>
        ///    
        private bool itemEnable2 =true;
        public bool ItemEnable2
        {
            get
            {
                return itemEnable2;
            }
            set
            {
                itemEnable2 = value;
                refreshflag = 1;
                this.Refresh();

            }
        }
        /// <summary>
        /// 第3按钮的使能

        /// </summary>
        private bool itemEnable3 =true;
        public bool ItemEnable3
        {
            get
            {
                return itemEnable3;
            }
            set
            {
                itemEnable3 = value;
                refreshflag = 1;
                this.Refresh();

            }
        }
        /// <summary>
        /// 第4按钮的使能

        /// </summary>
        private bool itemEnable4=true;
        public bool ItemEnable4
        {
            get
            {
                return itemEnable4;
            }
            set
            {
                itemEnable4 = value;
                refreshflag = 1;
                this.Refresh();

            }
        }
        private int itemCount = 2;
        /// <summary>
        /// 按钮的项数

        /// </summary>
        [System.ComponentModel.DefaultValue(2)]
        public int ItemCount
        {
            get
            {
                return itemCount;
            }
            set
            {
                if (value <= 0)
                    value = 1;
                if (itemCount != value)
                {
                    itemCount = value;
                    this.Refresh();
                }
            }
        }

        private bool border = true;
        /// <summary>
        /// 是否有边框

        /// </summary>
        [System.ComponentModel.DefaultValue(true)]
        public bool Border
        {
            get
            {
                return border;
            }
            set
            {
                if (border != value)
                {
                    border = value;
                    this.Invalidate();
                    this.Update();
                }
            }
        }

        //当前的聚焦索引,初始为第一个控件

        int currIndex = 0;
        /// <summary>
        /// 按钮的单击事件

        /// </summary>
        public event ButtonClick OnButtonClick;
        /// <summary>
        /// 按钮绘制完成事件
        /// </summary>
        public event AfterButtonPaint OnAfterButtonPaint;

        public int exeTime = 0;

        public ButtonCollection()
        {
                 this.KeyUp += new KeyEventHandler(ButtonCollection_KeyUp);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int startTime = System.Environment.TickCount;
            base.OnPaint(e);
            if (this.Width <= 0)
                return;
            //如果控件中按钮数量不等于ItemCount
            if (this.Controls.Count != itemCount)
            {
                //清除按钮
                this.Controls.Clear();
                //添加按钮
                for (int i = 0; i < itemCount; i++)
                {
                    ImageButton imageButton = new ImageButton();
                    imageButton.Name = "imageButton" + i.ToString();
                    imageButton.TabIndex = i;
                    imageButton.Click += new EventHandler(imageButton_Click);
                    imageButton.OnAfterButtonPaint += new AfterButtonPaint(imageButton_OnAfterButtonPaint);
                    imageButton.Size = new System.Drawing.Size(this.Width / itemCount, this.Height);
                    imageButton.Location = new System.Drawing.Point(i * imageButton.Width, 0);
                    if (ItemUpImage != null && ItemUpImage.Images.Count > i && ItemUpImage.Images[i] != null)
                        imageButton.UpImage = ItemUpImage.Images[i];
                    if (ItemDownImage != null && ItemDownImage.Images.Count > i && ItemDownImage.Images[i] != null)
                        imageButton.DownImage = ItemDownImage.Images[i];
                    if (ItemDisableImage != null && ItemDisableImage.Images.Count > i && ItemDisableImage.Images[i] != null)
                       imageButton.DisableImage = ItemDisableImage.Images[i];
                    switch(i)
                    { case 0:
                            imageButton.Enabled = itemEnable1;
                            break;
                    case 1:
                            imageButton.Enabled = itemEnable2;
                            break;

                    case 2:
                            imageButton.Enabled = itemEnable3;
                            break;

                    case 3:
                            imageButton.Enabled = itemEnable4;
                            break;

                    }
                    imageButton.Border = border;
                    this.Controls.Add(imageButton);
                }
            }
            else
            {
              for (int i = 0; i < itemCount; i++)
                {  
                    ImageButton imageButton = (ImageButton)(this.Controls[i]);

                    if (refreshflag == 0)
                    {
                        if (ItemUpImage != null && ItemUpImage.Images.Count > i && ItemUpImage.Images[i] != null)
                            imageButton.UpImage = ItemUpImage.Images[i];
                        if (ItemDownImage != null && ItemDownImage.Images.Count > i && ItemDownImage.Images[i] != null)
                            imageButton.DownImage = ItemDownImage.Images[i];
                        if (ItemDisableImage != null && ItemDisableImage.Images.Count > i && ItemDisableImage.Images[i] != null)
                            imageButton.DisableImage = ItemDisableImage.Images[i];
                    }
                     if (refreshflag == 1)
                    {
                        switch (i)
                        {
                            case 0:
                                imageButton.Enabled = itemEnable1;
                                break;
                            case 1:
                                imageButton.Enabled = itemEnable2;
                                break;

                            case 2:
                                imageButton.Enabled = itemEnable3;
                                break;

                            case 3:
                                imageButton.Enabled = itemEnable4;
                                break;
                        }
                    }
                    imageButton.Border = border;
                    imageButton.Refresh();
                }
            }
            exeTime = System.Environment.TickCount - startTime;
        }

        void ButtonCollection_KeyUp(object sender, KeyEventArgs e)
        {
            if (!this.Focused)
                return;
            if (e.KeyCode == Keys.Up)
            {
                if (currIndex == 0)
                    currIndex = itemCount - 1;
                else
                    currIndex = currIndex - 1;
                ItemGetFocus();
            }
            if (e.KeyCode == Keys.Down)
            {
                if (currIndex == itemCount - 1)
                    currIndex = 0;
                else
                    currIndex = currIndex + 1;
                ItemGetFocus();
            }
            if (e.KeyCode == Keys.Enter)
            {
                if (OnButtonClick != null)
                    OnButtonClick((ImageButton)this.Controls[currIndex], currIndex);
            }
        }

        private void ItemGetFocus()
        {
            ImageButton imageButton = new ImageButton();
            foreach (Control ctl in this.Controls)
            {
                if (ctl.GetType().Name == "ImageButton")
                {
                    imageButton = (ImageButton)ctl;
                    if (imageButton.TabIndex > itemCount - 1)
                        break;
                    if (imageButton.TabIndex == currIndex)
                    {
                        if (imageButton.buttonState == ButtonStateEnum.Up)
                        {
                            imageButton.buttonState = ButtonStateEnum.Down;
                            imageButton.Invalidate();
                            imageButton.Update();
                        }
                    }
                    else
                    {
                        if (imageButton.buttonState == ButtonStateEnum.Down)
                        {
                            imageButton.buttonState = ButtonStateEnum.Up;
                            imageButton.Invalidate();
                            imageButton.Update();
                        }
                    }
                }
            }
        }


        void imageButton_OnAfterButtonPaint(Graphics g, int buttonIndex)
        {
            if (OnAfterButtonPaint != null)
            {
                OnAfterButtonPaint(g, buttonIndex);
            }
        }

        void imageButton_Click(object sender, EventArgs e)
        {
            this.Focus();
            if (OnButtonClick != null)
            {
                OnButtonClick(sender, ((ImageButton)sender).TabIndex);
            }
        }
    }
    public delegate void ButtonClick(object sender, int buttonIndex);

    public delegate void AfterButtonPaint(Graphics g, int buttonIndex);
}
